package cn.zhuama.org.service.impl;

import cn.zhuama.basic.content.Content;
import cn.zhuama.basic.query.BaseQuery;
import cn.zhuama.basic.service.impl.BaseServiceImpl;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.basic.util.StrUtils;
import cn.zhuama.dto.SmsCodeDto;
import cn.zhuama.exception.businessException;
import cn.zhuama.org.domain.ShopAuditLog;
import cn.zhuama.org.service.ISmsCodeService;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class SmsCodeServiceImpl implements ISmsCodeService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void regist(Map<String, String> map) {
        String phone = map.get("phone");
        String redisKey = map.get("imageCodeKey");
        String imageCode = map.get("imageCode");

        // 参数校验
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(redisKey) || StringUtils.isEmpty(imageCode)) {
            throw new businessException("参数非法");
        }

        // 图片验证码校验
        Object oldImageCode = redisTemplate.opsForValue().get(redisKey);
        if (!(oldImageCode != null && oldImageCode.toString().toUpperCase().equals(imageCode.toUpperCase()))) {
            throw new businessException("图片验证码输入错误");
        }

        // 短信验证码逻辑处理
        Object oldSmsCode = redisTemplate.opsForValue().get(Content.PHONE_REGISTER_PREFIX+ phone);
        String code = null;
        if (oldSmsCode != null) {
            String[] split = oldSmsCode.toString().split(":");
            if ((System.currentTimeMillis() - Long.valueOf(split[1])) / 1000 <= 60) {
                throw new businessException("一分钟内不要重复发短信");
            }
            code = split[0];
        }
        code = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(Content.PHONE_REGISTER_PREFIX+phone, code + ":" + System.currentTimeMillis(), 3, TimeUnit.MINUTES);
        System.out.println("您的短信验证码是：" + code);
    }

    @Override
    public void binderSmsCode(Map<String, String> map) {
        String phone = map.get("phone");


        // 参数校验
        if (StringUtils.isEmpty(phone) ) {
            throw new businessException("参数非法");
        }

        // 短信验证码逻辑处理
        Object oldSmsCode = redisTemplate.opsForValue().get(Content.PHONE_WXBINDER_PREFIX + phone);
        String code = null;
        if (oldSmsCode != null) {
            String[] split = oldSmsCode.toString().split(":");
            if ((System.currentTimeMillis() - Long.valueOf(split[1])) / 1000 <= 60) {
                throw new businessException("一分钟内不要重复发短信");
            }
            code = split[0];
        }
        code = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(Content.PHONE_WXBINDER_PREFIX + phone, code + ":" + System.currentTimeMillis(), 3, TimeUnit.MINUTES);
        System.out.println("您的短信验证码是：" + code);
    }
}